package com.iweb.controller;


import com.common.context.RequestMapping;
import com.iweb.pojo.User;
import com.iweb.service.UserService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 视图渲染 返回值 是 string
 * void 是 ajax
 */
@RequestMapping("user")
public class UserController {

    @RequestMapping("userLogin")
    public String login(User user, HttpServletRequest request){
        UserService userService = new UserService();
        User result =null;
        String account = user.getUser_name();
//        /* 判断输入的内容是哪个字段 */
//        char[] chars = account.toCharArray();
//        int j = 0;
//        for (int i = 0; i < account.length(); i++) {
//            // 包含字母;65-90大写字母,97-122小写字母
//            if((chars[i] > 65 && chars[i] <90) || (chars[i] > 97 && chars[i] <122)){
//                j = 2;
//                break;
//            }
//            // 全是数字0-9;
//            if(chars[i] > 48 && chars[i] <58){
//                j = 1;
//            }else {
//                j = 0;
//                break;
//            }
//        }
        Pattern patternMailBox  = Pattern .compile( "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$" ); // 正则表达的式匹配邮箱
        Pattern patternPhone = Pattern.compile("^1[0-9]\\d{9}$");// 正则表达式匹配手机号
        Matcher matcherMailBox = patternMailBox.matcher(account);
        Matcher matcherPhone = patternPhone.matcher(account);
        if(matcherPhone.matches()){
            user.setUser_name(null);
            user.setUser_phone(account);
            result = userService.findUser("user_phone",user.getUser_phone());
        }else if(matcherMailBox.matches()){
            user.setUser_name(null);
            user.setUser_email(account);
            result = userService.findUser("user_email",user.getUser_email());
        }else {
            result = userService.findUser("user_name",user.getUser_name());
            // 业务判断
            if (user.getUser_name().startsWith("admin")) {
//            AdminController adminController = new AdminController();
//            Admin admin = adminController.login(user,request);
                request.setAttribute("message", "用户为管理员");
                return "/admin/index.jsp";
            }
        }
        if (result == null) {
            request.setAttribute("message", "用户不存在");
            return "/user/userLogin.jsp";
        }
        // 判断密码
        if (!user.getUser_password().equals(result.getUser_password())) {
            request.setAttribute("message", "密码错误");
            return "/user/userLogin.jsp";
        }
        request.setAttribute("message", "登录成功");
        // 保存用户到session
        HttpSession session = request.getSession();
        session.setAttribute("user", result);
        return "/user/userLogin.jsp";
    }

    /* 手机号动态码登录 */
    @RequestMapping("loginUserByCode")
    public String loginUserByCode(User user, HttpServletRequest request){
        UserService userService = new UserService();
        User result = userService.findUserByPhone(user.getUser_phone());
        if (result == null) {
            request.setAttribute("message", "用户不存在");
            return "/user/userLoginPhone.jsp";
        }else{
            StringBuilder str = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < 6; i++) {
                str.append(random.nextInt(10));
            }
            String code = str.toString();
            // 保存用户到session
            request.setAttribute("phone",user.getUser_phone());
            request.setAttribute("message",code);
            HttpSession session = request.getSession();
            session.setAttribute("user", result);
//            session.setAttribute("message",code);
//            session.setAttribute("phone",user.getUser_phone());
            return "/user/userLoginPhone.jsp";
        }
    }

    // 新增用户
    @RequestMapping("addUser")
    public String addAdmin(User user,HttpServletRequest request) {
        UserService userService = new UserService();
        boolean flag = userService.addUser(user);
        if (flag) {
            // 注册成功,跳转至主页
            request.setAttribute("message", "注册成功");
            return "/index/Index.jsp";
        } else {
            // 有重复的 ，跳转至登录页
            request.setAttribute("message", "用户已存在");
            return "/user/userLogin.jsp";
        }
    }

    /* 手机号查单 */
    @RequestMapping("selectUserByPhone")
    public String selectUserByPhone(User user, HttpServletRequest request){
        UserService userService = new UserService();
        User result = userService.findUserByPhone(user.getUser_phone());
        if (result == null) {
            request.setAttribute("message", "用户不存在");
            return "/user/select.jsp";
        }else{
            StringBuilder str = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < 6; i++) {
                str.append(random.nextInt(10));
            }
            String code = str.toString();

            // 保存用户到session
            request.setAttribute("phone",user.getUser_phone());
            request.setAttribute("message",code);
            HttpSession session = request.getSession();
            session.setAttribute("user", result);
//            session.setAttribute("message",code);
//            session.setAttribute("phone",user.getUser_phone());
            return "/user/select.jsp";
        }
    }
    /* 找回密码 */
    @RequestMapping("retrievePassword")
    public String retrievePassword(User user, HttpServletRequest request){
        UserService userService = new UserService();
        User result = userService.findUserByPhone(user.getUser_phone());
        if (result == null) {
//            HttpSession session = request.getSession();
//            session.setAttribute("message", "用户不存在!");
            request.setAttribute("message", "用户不存在!");
            return "/user/retrievePassword.jsp";
        }
        // 判断密保
        if (!user.getUser_question1().equals(result.getUser_question1())) {
            request.setAttribute("message","回答错误！");
//            HttpSession session = request.getSession();
//            session.setAttribute("message", "回答错误！");
            return "/user/retrievePassword.jsp";
        }
        if (!user.getUser_question2().equals(result.getUser_question2())) {
            request.setAttribute("message","回答错误!");
//            HttpSession session = request.getSession();
//            session.setAttribute("message", "回答错误！");
            return "/user/retrievePassword.jsp";
        }
        // 返回密码，并返回至登陆页
        request.setAttribute("message","恭喜您，已找回密码！您的密码为："+result.getUser_password());
        return "/user/userLogin.jsp";
    }
}

